package com.zyh.scs.admin.api.utils;

import cn.hutool.core.util.StrUtil;
import com.zyh.scs.admin.api.constant.TransactionTypeEnum;
import com.zyh.scs.admin.api.constant.UserTypeEnum;
import com.zyh.scs.admin.api.vo.account.TransactionGroupVO;
import com.zyh.scs.common.core.exception.OpenException;
import com.zyh.scs.common.core.exception.OpenResponseCode;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author hwh
 * @ClassName: 用户类型转换公共方法$
 * @Description: $
 * @Date $
 * @Version 1.0
 */
public class UserTypeToNameUtil {

    /**
     * 将用户类型  转换为用户类型名称
     *
     * @param userType 用户类型枚举
     * @return
     */
    public static String getUserType(Integer userType) {
        String userTypeName = null;
        switch (userType) {
            case 1:
                userTypeName = UserTypeEnum.USER_INSIDE.getDesc();
                break;
            case 2:
                userTypeName = UserTypeEnum.USER_OUTSET.getDesc();
                break;
            case 3:
                userTypeName = UserTypeEnum.USER_PROBATION.getDesc();
                break;
            case 4:
                userTypeName = UserTypeEnum.USER_RECHARGE.getDesc();
                break;
            case 5:
                userTypeName = UserTypeEnum.USER_BIT_UNIT.getDesc();
                break;
            case 6:
                userTypeName = UserTypeEnum.USER_CANTEEN.getDesc();
                break;
            case 7:
                userTypeName = UserTypeEnum.USER_OTHER.getDesc();
                break;
            case 8:
                userTypeName = UserTypeEnum.USER_EXTERIOR.getDesc();
                break;
            default:
                break;
        }
        return userTypeName;
    }


    /**
     * 将用户类型名称  转换为用户类型
     *
     * @param userTypeName 用户类型枚举
     * @return
     */
    public static Integer getUserTypeName(String userTypeName) {
        Integer userType = null;
        switch (userTypeName) {
            case "内部员工":
                userType = UserTypeEnum.USER_INSIDE.getCode();
                break;
            case "委外员工":
                userType = UserTypeEnum.USER_OUTSET.getCode();
                break;
            case "见习员工":
                userType = UserTypeEnum.USER_PROBATION.getCode();
                break;
            case "充值无管理费人员":
                userType = UserTypeEnum.USER_RECHARGE.getCode();
                break;
            case "委外单位":
                userType = UserTypeEnum.USER_BIT_UNIT.getCode();
                break;
            case "食堂职工":
                userType = UserTypeEnum.USER_CANTEEN.getCode();
                break;
            case "其他":
                userType = UserTypeEnum.USER_OTHER.getCode();
                break;
            case "外部":
                userType = UserTypeEnum.USER_EXTERIOR.getCode();
                break;
            default:
                break;
        }
        return userType;
    }

    /**
     * 将交易流水类型  转换为名称
     * 1满客宝充值 2满客宝消费 3满客宝退款 4京东锦礼消费 5京东锦礼退款 6后台充值 7好店消费 8好店退款 9后台退款 10后台支付
     * @param transactionType 交易流水类型枚举
     * @return
     */
    public static String getTransactionType(String transactionType) {
        String transactionTypeName = null;
        switch (transactionType) {
            case "1":
                transactionTypeName = TransactionTypeEnum.MANKEBAO_RECHARGE.getDesc();
                break;
            case "2":
                transactionTypeName = TransactionTypeEnum.MANKEBAO_CONSUMPTION.getDesc();
                break;
            case "3":
                transactionTypeName = TransactionTypeEnum.MANKEBAO_REFUND.getDesc();
                break;
            case "4":
                transactionTypeName = TransactionTypeEnum.JD_CONSUMPTION.getDesc();
                break;
            case "5":
                transactionTypeName = TransactionTypeEnum.JD_REFUND.getDesc();
                break;
            case "6":
                transactionTypeName = TransactionTypeEnum.BACK_RECHARGE.getDesc();
                break;
            case "7":
                transactionTypeName = TransactionTypeEnum.WARN_RECHARGE.getDesc();
                break;
            case "8":
                transactionTypeName = TransactionTypeEnum.WARN_DEDUCTION.getDesc();
                break;
            case "9":
                transactionTypeName = TransactionTypeEnum.BACK_DEDUCTION.getDesc();
                break;
            case "10":
                transactionTypeName = TransactionTypeEnum.BACK_PAY.getDesc();
                break;
            default:
                break;
        }
        return transactionTypeName;
    }

    /**
     * 生成16随机数  作为交易流水号
     *
     * @return
     */
    public static String setRechargeStream() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(16);
        for (int i = 0; i < 16; i++) {
            sb.append(random.nextInt(10)); // 生成0-9的随机数
        }
        return sb.toString();
    }

    public static boolean canConvertToLong(String param) {
        try {
            Long.parseLong(param);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static Long convertToLong(String param) {
        try {
            return Long.parseLong(param);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * 转开始时间LocalDateTime
     *
     * @param beginTime 格式为年-月-日
     * @return
     */
    public static LocalDateTime stringToLocalBeginTime(String beginTime) {
        try {
            if (StrUtil.isNotBlank(beginTime)) {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String timeEx = " 00:00:00";
                return LocalDateTime.parse(beginTime + timeEx, formatter);
            }
        } catch (Exception e) {
            throw new OpenException(OpenResponseCode.TIME_FAIL);
        }

        return null;
    }

    /**
     * 转结束时间LocalDateTime
     *
     * @param endTime 格式为年-月-日
     * @return
     */
    public static LocalDateTime stringToLocalEndTime(String endTime) {
        try {
            if (StrUtil.isNotBlank(endTime)) {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String timeEx = " 23:59:59";
                return LocalDateTime.parse(endTime + timeEx, formatter);
            }
        } catch (Exception e) {
            throw new OpenException(OpenResponseCode.TIME_FAIL);
        }
        return null;
    }

    /**
     * 将Workbook文件转为输入流
     *
     * @param workbook
     * @return
     */
    public static InputStream getExcelInputSteam(Workbook workbook) {
        ByteArrayInputStream inputStream = null;
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            workbook.write(os);

            byte[] b = os.toByteArray();
            inputStream = new ByteArrayInputStream(b);

            os.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return inputStream;
    }

    /**
     * string转 LocalDateTime
     *
     * @param time
     * @return
     */
    public static LocalDateTime getStringToTime(String time) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(time, formatter);
    }

    /**
     * LocalDateTime转时间戳
     *
     * @param time
     * @return
     */
    public static String getEpochSecond(LocalDateTime time) {

        String timeS = "";
        if (time != null) {
            // 指定时区转换为 ZonedDateTime
            ZoneId zoneId = ZoneId.systemDefault();
            // 使用系统默认时区
            ZonedDateTime zonedDateTime = time.atZone(zoneId);

            // 转换为 Instant
            Instant instant = zonedDateTime.toInstant();

            // 获取时间戳，单位为毫秒
            long timestamp = instant.getEpochSecond();

            timeS = String.valueOf(timestamp);
        }

        return timeS;
    }

    /**
     * map 封装
     *
     * @param transactionGroupVOMap
     * @return
     */
    public static Map<Integer, BigDecimal> getMapToBig(Map<Integer, TransactionGroupVO> transactionGroupVOMap) {
        Map<Integer, BigDecimal> bigDecimalMap = new HashMap<>();
        bigDecimalMap.put(1, transactionGroupVOMap.get(1) != null ? transactionGroupVOMap.get(1).getTotal() : BigDecimal.ZERO);
        bigDecimalMap.put(2, transactionGroupVOMap.get(2) != null ? transactionGroupVOMap.get(2).getTotal() : BigDecimal.ZERO);
        bigDecimalMap.put(3, transactionGroupVOMap.get(3) != null ? transactionGroupVOMap.get(3).getTotal() : BigDecimal.ZERO);
        bigDecimalMap.put(4, transactionGroupVOMap.get(4) != null ? transactionGroupVOMap.get(4).getTotal() : BigDecimal.ZERO);
        bigDecimalMap.put(5, transactionGroupVOMap.get(5) != null ? transactionGroupVOMap.get(5).getTotal() : BigDecimal.ZERO);
        bigDecimalMap.put(6, transactionGroupVOMap.get(6) != null ? transactionGroupVOMap.get(6).getTotal() : BigDecimal.ZERO);
        bigDecimalMap.put(7, transactionGroupVOMap.get(7) != null ? transactionGroupVOMap.get(7).getTotal() : BigDecimal.ZERO);
        bigDecimalMap.put(8, transactionGroupVOMap.get(8) != null ? transactionGroupVOMap.get(8).getTotal() : BigDecimal.ZERO);
        bigDecimalMap.put(10, transactionGroupVOMap.get(10) != null ? transactionGroupVOMap.get(10).getTotal() : BigDecimal.ZERO);

        return bigDecimalMap;

    }
}
